import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JFrame;


public class Drawer extends JFrame implements MouseMotionListener,MouseListener,KeyListener
{
	private Object mouseLock= new Object();
	private Graphics g;
	public Graphics2D g2;
	private Graphics2D gimage;
	private BufferedImage bimage;
	private int mouseX;
	private int mouseY;
	private int currentTurret=0;
	private boolean finalized;
	private Environment e;
	private boolean isMenu = true;
	private boolean isUpgrading = true;

	@SuppressWarnings("deprecation")
	public Drawer(Environment e){
		this.e=e;
		setBounds(0,0,e.getWidth(),e.getHeight());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		g = getGraphics();
		g2 = (Graphics2D)g;
		bimage = new BufferedImage(e.getWidth(), e.getHeight(), BufferedImage.TYPE_INT_RGB);
		gimage = bimage.createGraphics();
		addMouseMotionListener(this);
		addMouseListener(this);
		addKeyListener(this);
		setCursor(CROSSHAIR_CURSOR);
		setResizable(false);
	}

	public void Paint()
	{
		while(isMenu){
			drawMenu();
		}
		while(isUpgrading){
			drawUpgradeScreen();
		}
		gimage.setColor(new Color(135,206,235));
		gimage.fillRect(0,0,e.getWidth(), e.getHeight());
		for(Object o : e.getaList())
		{
			Paintable p=(Paintable)o;
			p.Paint(gimage);
		}
		synchronized(mouseLock)
		{
			try{
				for(Object o : e.getbList())
				{
					Paintable p=(Paintable)o;
					p.Paint(gimage);
				}
			}
			catch(Exception e){
				System.out.println("STUPID ERROR");
			}
		}
		for(Object o : this.e.gettList())
		{
			Paintable p=(Paintable)o;
			p.Paint(gimage);
		}
		gimage.drawString("Score: "+e.getScore(), 10,40);
		gimage.drawString("Health: "+e.getHealth(), 10, 60);
		String line = "Accuracy: ";
		if(e.getHitBullets()!=0){
			line+=((e.getHitBullets()+0.0)/(e.gettBullets()+0.0)*100)+"%";
		}
		gimage.drawString(line, 10, 80);
		this.g2.drawImage(bimage, null, 0, 0);

	}


	public void drawMenu(){
		this.gimage.setColor(new Color(0,0,0));
		this.gimage.fillRect(0, 0, e.getWidth(),e.getHeight());
		this.gimage.setColor(new Color(255,255,255));
		this.gimage.drawString("This is a game where you shoot meteors, aim and shoot by clicking the mouse.", 100, 220);
		this.gimage.drawString("Press Space to Continue", 260,250);
		this.g2.drawImage(bimage,null,0,0);
	}

	public void drawUpgradeScreen(){
		this.gimage.setColor(new Color(0,0,0));
		this.gimage.fillRect(0, 0, e.getWidth(), e.getHeight());
		this.gimage.setColor(Color.white);
		this.gimage.drawString("This is the upgrade screen",260,250);
		int counter=0;
		ArrayList<Turret> thisTList = new ArrayList<Turret>();
		thisTList.addAll(e.gettList());
		for(Turret t : e.gettList()){
			if(counter==currentTurret)
			{
				Turret newT = new Turret(mouseX,mouseY,e);
				newT.Paint(gimage);
				if(finalized){
					thisTList.remove(t);
					currentTurret=-1;
					thisTList.add(newT);
				}
			}
			else
			{
				t.Paint(gimage);
				t.act(mouseX, mouseY);
			}
			counter++;
		}
		e.settList(thisTList);
		this.g2.drawImage(bimage,null,0,0);
		this.e.setStartTime(System.currentTimeMillis());
	}

	public void mouseDragged(MouseEvent e) {
		synchronized(mouseLock){
			setMouseX(e.getX());
			setMouseY(e.getY());		
		}
	}

	public void mouseMoved(MouseEvent e) {
		synchronized(mouseLock){
			setMouseX(e.getX());
			setMouseY(e.getY());	
		}
	}


	public void mousePressed(MouseEvent a){
		synchronized(mouseLock)
		{
			for(Turret t : e.gettList())
			{
				t.setFireing(true);
			}		
		}
		
		if(isUpgrading)
		{
			finalized=true;
		}
	}
	/**
	 * @return the mouseX
	 */
	public int getMouseX() {
		return mouseX;
	}

	/**
	 * @param mouseX the mouseX to set
	 */
	public void setMouseX(int mouseX) {
		this.mouseX = mouseX;
	}

	/**
	 * @return the mouseY
	 */
	public int getMouseY() {
		return mouseY;
	}

	/**
	 * @param mouseY the mouseY to set
	 */
	public void setMouseY(int mouseY) {
		this.mouseY = mouseY;
	}

	public void keyTyped(KeyEvent e){
		char letter = e.getKeyChar();
		if(letter==' ')
		{
			setMenu(false);
			setUpgrading(false);
		}
		
	}

	public boolean getMenu(){
		return isMenu;
	}

	public void setMenu(boolean t){
		isMenu=t;
	}

	/**
	 * @return the isUpgrading
	 */
	public boolean isUpgrading() {
		return isUpgrading;
	}

	/**
	 * @param isUpgrading the isUpgrading to set
	 */
	public void setUpgrading(boolean isUpgrading) {
		this.isUpgrading = isUpgrading;
	}

	//Unused Methods
	public void mouseClicked(MouseEvent a) {}
	public void mouseEntered(MouseEvent a) {}
	public void mouseExited(MouseEvent a) {}


	public void mouseReleased(MouseEvent a) {
		synchronized(mouseLock)
		{
			for(Turret t : e.gettList())
			{
				t.setFireing(false);
			}		
		}
		
		if(isUpgrading)
		{
			finalized=false;
		}
	}

	public void keyPressed(KeyEvent e) 
	{
		
	}

	public void keyReleased(KeyEvent e) 
	{
		
		char letter=e.getKeyChar();
		if((letter=='t') & isUpgrading==true)
		{
			this.currentTurret++;
			this.currentTurret%=this.e.gettList().size();
		}

	}





}
